package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.adapter.PersonalInfoAddressAdapter;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.CountryDataResponse;
import com.redwine.app.net.responses.DownOptionsBean;
import com.redwine.app.net.responses.ProvinceDataResponse;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.NetUtil;
import com.redwine.app.view.EmptyView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.redwine.app.PersonalBasicInformationActivity.RESULT_CODE;
import static com.redwine.app.utils.Utils.showToast;

/**
 * 选择国家和所在地界面
 * 作者：gassion on 17/8/31 23:26
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class PersonalInfoAddressActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.list_view_address)
    private ListView listView;
    @ViewInject(R.id.empty_view)
    private EmptyView emptyView;
    private boolean isCountries = false;
    private PersonalInfoAddressAdapter personalInfoAddressAdapter;
    public static final String COUNTRIES_FLAG = "COUNTRIES_FLAG";
    public static final String SET_DATA = "SET_DATA";
    public List<DownOptionsBean> downOptionsBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info_address);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            isCountries = intent.getBooleanExtra(COUNTRIES_FLAG, false);
        } else {
            finish();
        }
        headerTitle.setText("");
        layoutBack.setVisibility(View.VISIBLE);
        personalInfoAddressAdapter = new PersonalInfoAddressAdapter(this);
        listView.setAdapter(personalInfoAddressAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DownOptionsBean downOptionsBean = (DownOptionsBean) personalInfoAddressAdapter.getItem(position);
                Intent intent = new Intent();
                intent.putExtra(SET_DATA, downOptionsBean);
                setResult(RESULT_CODE, intent);
                finish();
            }
        });
        listView.setEmptyView(emptyView);
        emptyView.bind();
        if (isCountries) {
            requestCountryData();
        } else {
            requestProvinceData();
        }
    }

    @OnClick({R.id.layout_back})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;
        }
    }


    /**
     * 获取省
     */
    private void requestProvinceData() {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            setErrorView(emptyView, null);
            return;
        }

        showWaiting();
        String url = NetworkConstants.GET_PROVINCE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        GsonRequest<ProvinceDataResponse> request = new GsonRequest<>(url, Request.Method.POST,
                ProvinceDataResponse.class, false, params, new Response.Listener<ProvinceDataResponse>() {
            @Override
            public void onResponse(ProvinceDataResponse response) {
                dismissWaiting();
                if (response.isSuccess()) {
                    ProvinceDataResponse.DataEntity responseData = response.getData();
                    if (null != responseData && null != responseData.getCity() && !responseData.getCity().isEmpty()) {
                        downOptionsBeanList = responseData.getCity();
                        personalInfoAddressAdapter.setData(downOptionsBeanList);
                    } else {
                        setEmptyView(emptyView, null);
                    }
                } else {
                    setEmptyView(emptyView, null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
                showToast(R.string.generic_check);
                setErrorView(emptyView, null);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }

    /**
     * 获取国家
     */
    private void requestCountryData() {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            setErrorView(emptyView, null);
            return;
        }

        showWaiting();
        String url = NetworkConstants.COUNTRY_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        GsonRequest<CountryDataResponse> request = new GsonRequest<>(url, Request.Method.POST,
                CountryDataResponse.class, false, params, new Response.Listener<CountryDataResponse>() {
            @Override
            public void onResponse(CountryDataResponse response) {
                dismissWaiting();
                if (response.isSuccess()) {
                    CountryDataResponse.DataEntity responseData = response.getData();
                    if (null != responseData && null != responseData.getCountry() && !responseData.getCountry().isEmpty()) {
                        List<CountryDataResponse.DataEntity.CountryEntity> country = responseData.getCountry();
                        for (int i = 0; i < country.size(); i++) {
                            DownOptionsBean downOptionsBean = new DownOptionsBean();
                            downOptionsBean.setName(country.get(i).getName());
                            downOptionsBean.setId(country.get(i).getId());
                            downOptionsBeanList.add(downOptionsBean);
                        }
                        personalInfoAddressAdapter.setData(downOptionsBeanList);
                    } else {
                        setEmptyView(emptyView, null);
                    }
                } else {
                    setEmptyView(emptyView, null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
                showToast(R.string.generic_check);
                setErrorView(emptyView, null);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }


    @Override
    protected void emptyOnclick() {
        emptyView.bind();
        if (isCountries) {
            requestCountryData();
        } else {
            requestProvinceData();
        }
    }

}
